package com.link.play.fit.ai.ui.adjust.adjustPage

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.widget.ViewPager2
import com.link.play.fit.ai.GlobalConfig
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.bean.BaseEventData
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AdjustPageActivityBinding
import com.link.play.fit.ai.ext.d
import com.link.play.fit.ai.ui.adjust.adjustPage.fragment.AdjustPageFragment1
import com.link.play.fit.ai.ui.adjust.adjustPage.fragment.AdjustPageFragment2
import com.link.play.fit.ai.ui.adjust.adjustPage.fragment.AdjustPageFragment3
import com.link.play.fit.ai.ui.adjust.adjustPage.fragment.AdjustPageFragment4
import com.link.play.fit.ai.ui.adjust.adjustPage.fragment.AdjustPageFragment5
import com.link.play.fit.ai.ui.main.adapter.AdjustPageAdapter
import com.link.play.fit.ai.ui.main.adapter.HomeChildAdapter
import com.link.play.fit.ai.ui.main.fragment.AnalyticsFg
import com.link.play.fit.ai.ui.main.fragment.ControlFg
import com.link.play.fit.ai.ui.main.fragment.HomeFg

import org.greenrobot.eventbus.EventBus


class AdjustPageActivity :
    BaseActivity<AdjustPageActivityBinding>(AdjustPageActivityBinding::inflate) {
    private val viewModel: AdjustPageModel by lazy {
        ViewModelProvider(this)[AdjustPageModel::class.java]
    }

    private val mList: MutableList<Fragment> = mutableListOf(
        AdjustPageFragment1(),
        AdjustPageFragment2(), AdjustPageFragment3(), AdjustPageFragment4(), AdjustPageFragment5()
    )
    private val mGoalsData by lazy {
        GlobalConfig.gloalsData
    }
    //    private var recentlyGoalData: RecentlyGoalData? = null
    private var userGuide: UserGuide? = null

    private var mPosition = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initData()
        initListener()
        initLiveData()
    }

    private fun initData() {
        userGuide = intent.getSerializableExtra("bean") as UserGuide?
        userGuide?.let {
            viewModel.updateData(it)
        }

//        binding.adjustViewPager2.offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
//        binding.adjustViewPager2.adapter = AdjustPageAdapter(supportFragmentManager, lifecycle)
        binding.adjustViewPager2.apply {
            offscreenPageLimit = 3
            isUserInputEnabled = false
            adapter = AdjustPageAdapter(supportFragmentManager, lifecycle)
//            adapter = HomeChildAdapter(supportFragmentManager, lifecycle, fragmentList)
            registerOnPageChangeCallback(pageChangeCallback)
        }
        binding.adjustProgressBar.max = mList.size
        binding.adjustProgressBar.progress = mPosition + 1
        mGoalsData?.let {
            viewModel.tempGoalsData.value = it
        }
    }

    private fun initListener() {
        binding.backImage.setOnClickListener {
            if (mPosition > 0) {
                binding.adjustViewPager2.setCurrentItem(mPosition - 1, true)
            } else {
                finish()
            }
        }
        binding.nextText.setOnClickListener {
            if (mPosition >= mList.size - 1) {
                val goalsData = viewModel.getGoalData()
                EventBus.getDefault()
                    .post(BaseEventData(type = "byAdjustPage", recentlyGoalData = goalsData))
                finish()
            } else {

                binding.adjustViewPager2.setCurrentItem(mPosition + 1, true)
            }
        }
    }

    private fun initLiveData() {
        viewModel.fragmentValue.observe(this) { hasChanged ->
            binding.nextText.isEnabled = hasChanged
        }
    }


    private val pageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            // 当新页面被选中时触发
            kotlin.runCatching {
                mPosition = position
                binding.adjustProgressBar.progress = position + 1
                if (position >= mList.size - 1) {
                    binding.nextText.text = getString(R.string.auto_generate_goals)
                } else {
                    binding.nextText.text = getString(R.string.Next)
                }
            }
        }

        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
        }

        override fun onPageScrollStateChanged(state: Int) {
        }
    }

}